﻿using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Wicresoft.SMS.Common;


namespace Wicresoft.SMS.Gateway.Host
{
    public class WESMSProvider : ISMSProvider
    {
        IWESMSParams _paraments;
        public WESMSProvider(IWESMSParams paraments)
        {
            _paraments = paraments;
        }
        
        public SMSResult SendMsg(string moblie, string content)
        {
            int formatID = _paraments.formatID;

            string sToken = _paraments.StokenString;
            SMSResult smsResult = new SMSResult();
            smsResult.Status = SendStatus.SystemWrong;
            try
            {
                string getReturn = string.Empty;
                //单元测试时SMSServiceSoapClient参数为new BasicHttpBinding(),new EndpointAddress("http://www.wemediacn.net/webservice/smsservice.asmx" 实际运行时调用Appconfig
                using (WEService.SMSServiceSoapClient sc = new WEService.SMSServiceSoapClient("SMSServiceSoap"))
                //using (WEService.SMSServiceSoapClient sc = new WEService.SMSServiceSoapClient(new BasicHttpBinding(), new EndpointAddress("http://localhost:9999/fakesmsservice")))
                {
                    getReturn = sc.SendSMS(moblie, formatID, content, new DateTime(2010, 1, 1), sToken);
                    DBLog.Debug( "number: " + moblie +  "getReturn: " + getReturn);
                }
                if (getReturn == string.Empty || getReturn.Length < 5)
                    return null;
                if (getReturn.Substring(0, 2) == "OK")
                {
                    smsResult.Status = SendStatus.OK;
                    smsResult.MessageId = getReturn.Substring(4, getReturn.Length - 5);
                }
                else if (getReturn.Substring(0, 5) == "ERROR")
                {
                    switch (getReturn.Substring(6, 3))
                    {
                        case "100":
                            smsResult.Status = SendStatus.WrongToken;
                            break;
                        case "101":
                            smsResult.Status = SendStatus.NoRight;
                            break;
                        case "102":
                            smsResult.Status = SendStatus.NoMoney;
                            break;
                        case "103":
                            smsResult.Status = SendStatus.EmptyContent;
                            break;
                        case "200":
                            smsResult.Status = SendStatus.WrongMoblie;
                            break;
                        case "999":
                            smsResult.Status = SendStatus.SystemWrong;
                            break;
                    }

                }
            }
            catch (Exception ex)
            {
                Logger.Debug("WESERVICE SendMsg: "+ex.Message);
            }

            return smsResult;

        }

        public MsgResult QueryMsgState(string MassageId)
        {
            MsgResult msgResult = new MsgResult();
            XmlElement getReturn;
            try
            {
                using (WEService.SMSServiceSoapClient sc = new WEService.SMSServiceSoapClient("SMSServiceSoap"))
                //using (WEService.SMSServiceSoapClient sc = new WEService.SMSServiceSoapClient(new BasicHttpBinding(), new EndpointAddress("http://localhost:9999/fakesmsservice")))
                {
                    getReturn = sc.QuerySMSState(MassageId);
                    if (getReturn == null)
                    {
                        msgResult.status = MsgStates.Waiting; 
                        return msgResult;
                    }


                }
                XmlSerializer xsz = new XmlSerializer(typeof(NewDataSet));

                using (XmlTextReader xrd = new XmlTextReader(new StringReader(getReturn.OuterXml)))
                {
                    NewDataSet qureyReturn = xsz.Deserialize(xrd) as NewDataSet;
                    msgResult.mobile = qureyReturn.result.mobile;
                    msgResult.scheduleDate = qureyReturn.result.scheduleDate;
                    msgResult.deliverstate = qureyReturn.result.deliverstate;
                    msgResult.deliverdonetime = qureyReturn.result.deliverdonetime;
                }
                if (string.IsNullOrEmpty(msgResult.deliverstate))
                    msgResult.status = MsgStates.Waiting;
                else if (msgResult.deliverstate == "DELIVRD")
                    msgResult.status = MsgStates.Success;
                else
                    msgResult.status = MsgStates.Fail;
            }
            catch (Exception ex)
            {
                msgResult.status = MsgStates.Error;
                Logger.Debug("WESERVICE QueryMsgState: " + ex.Message);
                return msgResult;
            }
                
            return msgResult;
                
            
           
        }
    }
}


